﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Phone.RichTextBox.Documents;
using System.IO;
using Microsoft.Phone.Shell;

using C1.Phone.RichTextBox;
using Microsoft.Phone.Controls;
using C1.Util;

namespace ControlExplorer.Controls.RichTextBox
{
    public partial class DemoRichTextBox
        : UserControl, IExposeApplicationBarItems
    {
        C1BoldTool btnBold = new C1BoldTool();
        C1ItalicTool btnItalic = new C1ItalicTool();
        C1UnderlineTool btnUnderline = new C1UnderlineTool();

        C1LeftAlignTool btnLeftAlign = new C1LeftAlignTool();
        C1RightAlignTool btnRightAlign = new C1RightAlignTool();
        C1CenterAlignTool btnCenterAlign = new C1CenterAlignTool();
        C1JustifyTool btnJustifyAlign = new C1JustifyTool();

        ApplicationBarIconButton btnHyperlink = new ApplicationBarIconButton(new Uri("/Resources/AppBar/link.png", UriKind.Relative));
        HyperlinkPopup linkPopup;

        public DemoRichTextBox()
        {
            InitializeComponent();

            btnHyperlink.Text = "hyperlink";
            btnHyperlink.Click += btnHyperlink_Click;
            linkPopup = new HyperlinkPopup(rtb);

            Loaded += DemoRichTextBox_Loaded;
        }

        

        void DemoRichTextBox_Loaded(object sender, RoutedEventArgs e)
        {
            var stream = Application.GetResourceStream(new Uri("/ControlExplorer" + App.AssemblySuffix + ";component/Resources/simple.htm", UriKind.Relative)).Stream;
            var html = new StreamReader(stream).ReadToEnd();
            rtb.Html = html;
            rtb.NavigationMode = NavigationMode.Always;
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get
            {
                // ****************** first 4 elements will fill the ApplicationBar.Buttons collection

                //btnLeftAlign.IconUri = new Uri("/Resources/AppBar/rtb_underline.png", UriKind.Relative);
                //btnLeftAlign.RichTextBox = rtb;
                //yield return btnLeftAlign;

                //btnCenterAlign.IconUri = new Uri("/Resources/AppBar/rtb_underline.png", UriKind.Relative);
                //btnCenterAlign.RichTextBox = rtb;
                //yield return btnCenterAlign;

                //btnRightAlign.IconUri = new Uri("/Resources/AppBar/rtb_underline.png", UriKind.Relative);
                //btnRightAlign.RichTextBox = rtb;
                //yield return btnRightAlign;

                //btnJustifyAlign.IconUri = new Uri("/Resources/AppBar/rtb_underline.png", UriKind.Relative);
                //btnJustifyAlign.RichTextBox = rtb;
                //yield return btnJustifyAlign;


                // ****************** the rest of the elements will be added as MenuItems

                btnBold.IconUri = new Uri("/Resources/AppBar/rtb_bold.png", UriKind.Relative);
                btnBold.RichTextBox = rtb;
                yield return btnBold;

                btnItalic.IconUri = new Uri("/Resources/AppBar/rtb_italic.png", UriKind.Relative);
                btnItalic.RichTextBox = rtb;
                yield return btnItalic;

                btnUnderline.IconUri = new Uri("/Resources/AppBar/rtb_underline.png", UriKind.Relative);
                btnUnderline.RichTextBox = rtb;
                yield return btnUnderline;

                yield return btnHyperlink;

            }
        }

        #endregion

        void btnHyperlink_Click(object sender, EventArgs e)
        {
            // close keyboard from a UserControl
            PhoneApplicationPage parentPage = Util.GetParentOfType<PhoneApplicationPage>(this);
            if (parentPage != null)
            {
                parentPage.Focus();
            }

            // show popup
            linkPopup.ShowPopup();
        }

        private void rtb_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            var result = MessageBox.Show("The document is requesting to navigate to " + e.Hyperlink.NavigateUri, "Navigate", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                // navigate to urls loading web browser control
                WebBrowser browser = new WebBrowser();
                browser.Navigate(e.Hyperlink.NavigateUri);
                Content = browser;
            }
            else
            {
                // select hyperlink for editing
                rtb.Select(e.Hyperlink.ContentStart.TextOffset, e.Hyperlink.ContentRange.Text.Length);
            }
        }
    }

}

